//
//  OBViewController.m
//  WeChat
//
//  Created by 黄柳姣 on 2018/4/29.
//  Copyright © 2018年 obally. All rights reserved.
//
#import "OBViewController.h"
#import "OBNavigationController.h"

@interface OBViewController ()

@property(nonatomic,strong,readwrite) OBViewModel *viewModel;

@end

@implementation OBViewController
- (void)dealloc
{
    OBDealloc;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    OBViewController *viewController = [super allocWithZone:zone];
    @weakify(viewController);
    [[viewController rac_signalForSelector:@selector(viewDidLoad)]subscribeNext:^(id x) {
        @strongify(viewController);
        [viewController bindViewModel];
    }];
    return viewController;
}
- (instancetype)initWithViewModel:(OBViewModel *)viewModel
{
    self = [super init];
    if (self) {
        self.viewModel = viewModel;
    }
    return self;
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 隐藏导航栏细线
    self.viewModel.prefersNavigationBarBottomLineHidden?[(OBNavigationController *)self.navigationController hideNavigationBottomLine]:[(OBNavigationController *)self.navigationController showNavigationBottomLine];
    //配置键盘
    IQKeyboardManager.sharedManager.enable = self.viewModel.keyboardEnable;
    IQKeyboardManager.sharedManager.shouldResignOnTouchOutside = self.viewModel.shouldResignOnTouchOutside;
    
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.viewModel.willDisappearSignal sendNext:nil];
    
    // Being popped, take a snapshot
    if ([self isMovingFromParentViewController]) {
        self.snapshot = [self.navigationController.view snapshotViewAfterScreenUpdates:NO];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    /// iOS 11新增：adjustContentInset 和 contentInsetAdjustmentBehavior 来处理滚动区域
    ///
    if (@available(iOS 11.0, *)) {
        //        self.automaticallyAdjustsScrollViewInsets = YES;
    }else{
        self.automaticallyAdjustsScrollViewInsets = YES;
    }
    
    self.extendedLayoutIncludesOpaqueBars = YES;
    /// backgroundColor
//    self.view.backgroundColor = OB_MAIN_BACKGROUNDCOLOR;
    self.view.backgroundColor = OB_MAIN_BACKGROUNDCOLOR;
    
    /// 导航栏隐藏 只能在ViewDidLoad里面加载，无法动态
    self.fd_prefersNavigationBarHidden = self.viewModel.prefersNavigationBarHidden;
    
    /// pop手势
    self.fd_interactivePopDisabled = self.viewModel.interactivePopDisabled;
}
- (void)bindViewModel
{
    RAC(self,title) = RACObserve(self, viewModel.title);
    NSLog(@"title is  %@",self.viewModel.title);
    
    /// 绑定错误信息
    [self.viewModel.errors subscribeNext:^(NSError *error) {
        /// 这里可以统一处理某个错误，例如用户授权失效的的操作
        NSLog(@"...错误...");
    }];
    
    
}
#pragma mark - Orientation
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {return UIInterfaceOrientationMaskPortrait;}
- (BOOL)shouldAutorotate {return YES;}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {return UIInterfaceOrientationPortrait;}

#pragma mark - Status bar
- (BOOL)prefersStatusBarHidden {
    return NO;
}
- (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; }
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation { return UIStatusBarAnimationFade; }

@end
